package com.tom.architect02.zframework.binder3.ipc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

//import com.example.ipccore.request.RequestBean;
//import com.example.ipccore.request.RequestParamter;
import com.google.gson.Gson;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
public class DavidServiceManager   extends Service {
    //服务获取
    public static final int TYPE_GET = 1;
    //服务调用
    public static final int TYPE_INVOKE = 2;
    Gson gson = new Gson();
    CacheCenter cacheCenter = CacheCenter.getInstance();
    BBBinder bbBinder = new BBBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private DavidBinderInterface.Stub mBinder = new DavidBinderInterface.Stub() {
        //B进程 调用了      A进程 获取了  重写Binder 文件
        @Override
        public String reuqest(String reuqest) throws RemoteException {
            RequestBean requestBean = gson.fromJson(reuqest, RequestBean.class);
            int type = requestBean.getType();

            switch (type) {
                case TYPE_GET:
                    Object[] parameters = makeParameterObject(requestBean);
                    Method method = cacheCenter.getMethod(requestBean);
                    Object object=bbBinder.onTransact(method,parameters);
                    cacheCenter.putObject(requestBean.getClassName(), object); // 服务发现
                    break;
                case TYPE_INVOKE:
                    Log.i("maniu", "服务调用: "+reuqest);
//                    思想   api     代码   思想
                     object =   cacheCenter.getObject(requestBean.getClassName());
                    Method tempMethod = cacheCenter.getMethod(requestBean);
//                    参数    str
                    Object[] mParameters = makeParameterObject(requestBean);

                    Object result=bbBinder.onTransact(tempMethod,mParameters);
                    String data = gson.toJson(result);
                    return data;
            }

            return null;
        }
    };

    // 反序列化。
    private Object[] makeParameterObject(RequestBean requestBean) {
        Object[] mParameters = null;
        RequestParamter[] requestParamters = requestBean.getRequestParamters();
        if (requestParamters != null && requestParamters.length > 0) {
            mParameters = new Object[requestBean.getRequestParamters().length];
            for (int i = 0; i < requestParamters.length; i++) {
                RequestParamter requestParamter = requestParamters[i];
                Class<?> clazz = cacheCenter.getClassType(requestParamter.getParameterClassName());
//                    clazz   value  object
                mParameters[i] = gson.fromJson(requestParamter.getParameterValue(), clazz);
            }

        }else {
            mParameters = new Object[0];
        }
        return mParameters;
    }

}*/
